import shutil,pathlib,os

def get_zzdir(x,dirname):
    temp_list=[]
    for y in x[0].parent.iterdir():
        if y.name!=x[0].name :
            temp_list.append((y,dirname.joinpath(x[0].stem+'-'+y.name)))
    return temp_list  
if __name__=="__main__":    
    while True:
        #需要输入目录，如 d:\hundsun_files\*  hundsun_files目录下包含了所有的用于升级的文件。
        uf20path=input('输入需要合并包的上级目录,[q 退出]:')
        if uf20path == 'q':
            os._exit(0)
        elif pathlib.Path(uf20path).exists():
            base_dir=[x for x in pathlib.Path(uf20path).glob('*')]
            for y in base_dir:
                sub_dir=[x for x in y.glob('*')]
                #file_name=[z for z in sub_dir if z.is_file() and z.suffix =='.rar']
                #print(file_name[0].stem)
                #a=[w.rename(y.joinpath(y.name+'-'+w.stem)) for w in sub_dir if w.is_dir() and w.name=='增值程序']
                #a=[w.rename(y.joinpath(y.name+'-'+w.stem)) 
                for w in sub_dir: 
                    print(str(w),w.name,y)
                    if w.is_dir() and (w.name=='增值程序' or w.name='周边接口'):
                        new_file_name=y.name+'-'+w.stem
                        w.rename(w.parent.joinpath(new_file_name))
                        shutil.move(w.parent.joinpath(new_file_name),(y.resolve().parent.joinpath(new_file_name)))
                    if w.suffix == '.rar':
                        shutil.move(w,(y.resolve().parent.joinpath(w.name)))
                        
                is_empty=[x for x in y.glob('*')]
                if not is_empty:
                    y.rmdir()
            os._exit(0)
        else:
            print('文件路径不合法,请重新输入')
            
            
            

    